﻿using ENNote.Common;
using ENNote.Dto;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;

namespace ENNote.Controllers
{
    /// <summary>
    /// 笔记控制器
    /// </summary>
    public class NoteController : Controller
    {
        /// <summary>
        /// 创建临时笔记
        /// </summary>
        /// <param name="currentNotebook">当前笔记本</param>
        /// <returns></returns>
        [HttpPost("/note/add")]
        public ResData CreateNote([FromBody] AddTempNoteDto addTempNoteDto)
        {
            if (string.IsNullOrEmpty(addTempNoteDto.CurrentNotebook))
            {
                return new ResData(400, "请选择笔记本");
            }

            var name = DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmss");
            var path = Path.Combine(MyConfig.localData, addTempNoteDto.CurrentNotebook, (name + ".md"));
            try
            {
                var f = System.IO.File.Create(path);
                f.Dispose();
                return new ResData(200, "创建成功", name);
            }
            catch (Exception e)
            {
                return new ResData(500, "创建失败：" + e.Message);
            }
        }


        /// <summary>
        /// 打开笔记
        /// </summary>
        /// <param name="currentNotebook">当前笔记本</param>
        /// <param name="currentNote">当前笔记</param>
        /// <returns></returns>
        [HttpGet("/note/open")]
        public ResData OpenNote([FromQuery] string currentNotebook, [FromQuery] string currentNote)
        {
            if (string.IsNullOrEmpty(currentNotebook) || string.IsNullOrEmpty(currentNote))
            {
                return new ResData(400, "请选择要打开的笔记");
            }

            var path = Path.Combine(MyConfig.localData, currentNotebook, (currentNote + ".md"));
            try
            {
                // 读取笔记内容
                string noteContent = System.IO.File.ReadAllText(path);
                return new ResData(200, "读取成功", noteContent);
            }
            catch (Exception e)
            {
                return new ResData(500, "读取失败：" + e.Message);
            }
        }


        /// <summary>
        /// 保存笔记内容
        /// </summary>
        /// <param name="currentNotebook">当前笔记本</param>
        /// <param name="currentNote">当前笔记</param>
        /// <param name="content">笔记内容</param>
        /// <returns></returns>
        [HttpPost("/note/save")]
        public ResData SaveNote([FromBody] SaveNoteDto saveNoteDto)
        {
            if (string.IsNullOrEmpty(saveNoteDto.CurrentNotebook)
                || string.IsNullOrEmpty(saveNoteDto.CurrentNote)
                || string.IsNullOrEmpty(saveNoteDto.NoteTitle))
            {
                return new ResData(400, "请填写完整");
            }

            var path = Path.Combine(MyConfig.localData, saveNoteDto.CurrentNotebook, (saveNoteDto.CurrentNote + ".md"));
            // 新路径
            var newPath = Path.Combine(MyConfig.localData, saveNoteDto.CurrentNotebook, (saveNoteDto.NoteTitle + ".md"));
            try
            {
                // 写入笔记内容
                System.IO.File.WriteAllText(path, saveNoteDto.Content);
                // 文件重命名
                System.IO.File.Move(path, newPath);
                return new ResData(200, "保存成功");
            }
            catch (Exception e)
            {
                return new ResData(500, "保存失败：" + e.Message);
            }
        }


        /// <summary>
        /// 删除笔记
        /// </summary>
        /// <param name="currentNotebook">当前笔记本</param>
        /// <param name="currentNote">要删除的笔记</param>
        /// <returns></returns>
        [HttpDelete("/note/remove")]
        public ResData RemoveNote(string currentNotebook, string currentNote)
        {
            if(string.IsNullOrEmpty(currentNotebook) || string.IsNullOrEmpty(currentNote))
            {
                return new ResData(400, "请选中笔记");
            }

            var path = Path.Combine(MyConfig.localData, currentNotebook, (currentNote + ".md"));
            try
            {
                // 删除文件
                System.IO.File.Delete(path);
                return new ResData(200, "删除成功");
            }
            catch (Exception e)
            {
                return new ResData(500, "删除失败：" + e.Message);
            }
        }
    }
}
